<?php
namespace logic\sys\power;
class Menu{
    
    private $User;
    public function __construct(\logic\user\User $user){
        $this->User = $user;
    }

    /**
     * 自动添加一个菜单
     */
    public function autoAddMenu($param){
        $check = [
            'plat'      => ['check'=>'must','msg'=> '平台代号不能为空','reg'=>'int'],
            'menuurl'   => ['check'=>'must','msg'=> '菜单节点不能为空'],
        ];        
        $info = D('Menu','sysPower')->find(['plat' => $param['plat'], 'menuurl'=> $param['menuurl']]);
        if( $info )  return $info;
        $param['id'] = D('Menu','sysPower')->add($param);
        return $param;
    }

    /**
     * 添加菜单【添加无节点菜单或把已收录节点绑定到具体菜单下】
     */
    public function add($param){
        if(in_array($param['depth'],[3,4]) && $param['id']<1) logicE('该等级节点必须设置url');
        if($param['id']>0){
            return D('Menu','sys\power')->edit($param['id'],$param);
        }else{
            return D('Menu','sys\power')->add($param);
        }        
    }

    /**
     * 修改菜单
     */
    public function edit($id,$param){
        return D('Menu','sys\power')->edit($id,$param);        
    }

    /**
     * 删除菜单
     */
    public function del($id){        
        $info = D('Menu','sys\power')->find(array('pid'=>$id,'status'=>1));
        if($info) logicE('当前节点下拥有正常子节点，不允许删除');
        return D('Menu','sys\power')->edit($id,array('status'=>2));
    }

    /**
     * 获取菜单
     */
    public function getList($param){
        $param['page']  = 1;
        $param['limit'] = 999999;
        $list = D('Menu','sys\power')->getList($param);
        return $list;
        
    }

    /**
     *获取一个菜单信息
     */
    public function find($param){
        $info = D('Menu','sys\power')->find($param);
        return $info;
    }
}